package com.lixinlei.meetingfilm.consumer.ribbon;

import com.netflix.client.ClientException;
import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.client.http.HttpClientRequest;
import com.netflix.niws.client.http.RestClient;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Ribbon 的 hello-world 程序，可在配置文件中手动指定 ServerList；
 */
public class App {

    public static void main(String[] args) throws IOException, ClientException, URISyntaxException, InterruptedException {
//        method1();
        method2();
    }

    /**
     * server list 可动态调整
     * @throws IOException
     * @throws ClientException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    private static void method2() throws IOException, ClientException, URISyntaxException, InterruptedException{

        // 读取配置文件
        ConfigurationManager.loadPropertiesFromResources("lixinlei.properties");  // 1
        System.err.println(ConfigurationManager.getConfigInstance().getProperty("lixinlei-client.ribbon.listOfServers"));

        RestClient client = (RestClient) ClientFactory.getNamedClient("lixinlei-client");  // 2
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build(); // 3

        // 动态修改的serverList
        ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
        System.err.println(lb.getLoadBalancerStats());
        ConfigurationManager.getConfigInstance().setProperty(
                "lixinlei-client.ribbon.listOfServers", "www.qq.com:80,www.taobao.com:80"); // 5
        System.err.println("changing servers ...");
        Thread.sleep(3000); // 6
        for (int i = 0; i < 5; i++)  {
            HttpResponse response = client.executeWithLoadBalancer(request);
            System.err.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());
        }
        System.out.println(lb.getLoadBalancerStats()); // 7

        // 最好的情况就是Ribbon和注册中心结合使用
    }

    /**
     * server list 固定的情况
     * @throws IOException
     * @throws URISyntaxException
     * @throws ClientException
     */
    private static void method1() throws IOException, URISyntaxException, ClientException {
        // 读取配置文件
        ConfigurationManager.loadPropertiesFromResources("lixinlei.properties");  // 1
        System.err.println(ConfigurationManager.getConfigInstance().getProperty("lixinlei-client.ribbon.listOfServers"));

        // 构建一个 HttpClient，serverList 固定的情况
        RestClient client = (RestClient) ClientFactory.getNamedClient("lixinlei-client");  // 2
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build(); // 3
        for (int i = 0; i < 5; i++)  {
            HttpResponse response = client.executeWithLoadBalancer(request); // 4
            System.err.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());
        }
    }

}
